package com.tigase.serviceImpl;/**
 * Created by tianfeng on 2017/7/20.
 */

import com.tigase.bean.TigaseUser;
import com.tigase.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author tianfeng
 * @create 2017-07-20 13:21
 **/
@Service
public class FriendServiceImpl implements FriendService{
    @Autowired
    private JdbcTemplate JdbcTemplate;

    @Override
    public List<Map<String,String>> getFriendList(String userName) {
        List<Map<String,String>> user = new LinkedList<>();

        String sql = "Select user_name,user_nick from t_user where user_id in \n" +
                "(SELECT friend_id from t_friends INNER JOIN t_user on t_user.user_id=t_friends.user_id WHERE t_user.user_name= ? and t_friends.friend_status= '1' )  ";
        List<Map<String, Object>> result = JdbcTemplate.queryForList(sql, new Object[]{userName});
        if (result.size() > 0) {
            for (int i = 0; i < result.size(); i++) {
                Map<String,String> maps=new LinkedHashMap<>();
                String sUserName = (String) result.get(i).get("user_name");
                String sUserNick = (String) result.get(i).get("user_nick");
                maps.put("userName",sUserName);
                maps.put("userNick",sUserNick);
                user.add(maps);
            }

        }
        return user;
    }
    public int delFriendFromList(String friendName) {
        String sql = "delete from t_friends WHERE friend_id =(select user_id from t_user where user_name= ? )";
        int result = JdbcTemplate.update(sql, new Object[]{friendName});
        System.out.println("删除好友" +result);
        return 0;
    }

    public int insertFriend(String userName,String friendName,String status) {
        String sql="insert into t_friends(user_id,friend_id,friend_status) values(?,?,?)";
        if(status==null){
            status="0";
        }
        int result=JdbcTemplate.update(sql,userName,friendName,status);
        System.out.println("添加好友" +result);
        return result;
    }


    public int updateFriend(String userName, String friend) {
        String sql="update t_friends SET friend_status= '1' WHERE  user_id= ? and friend_id= ?";
        int result = JdbcTemplate.update(sql,new Object[]{userName,friend});
        return 0;
    }


    public List<Map<String, Object>> getFriend(String userName, String friend) {
        String sql="SELECT * FROM t_friends WHERE  user_id= ? and friend_id= ?";
        List<Map<String, Object>> result=JdbcTemplate.queryForList(sql,userName,friend);
        return result;
    }
}
